import styles from "./TicketQr.module.css";
import Button from "@/components/ui/Button/Button";
import Image from "next/image";

export default function TicketQr({qrCodeUrl}: { qrCodeUrl: string }) {
    const handleDownloadTicket = async () => {
        try {
            const downloadLink = document.createElement('a');
            downloadLink.href = qrCodeUrl;
            downloadLink.download = `${qrCodeUrl.split('/').pop()}.png`;
            downloadLink.style.display = 'none'
            document.body.appendChild(downloadLink);
            downloadLink.click();
            document.body.removeChild(downloadLink);
        } catch (error) {
            console.error('Ошибка при скачивании билета:', error);
        }
    };

    return (
        <>
            <div className={styles.QrContainer}>
                <Image src={qrCodeUrl} width={330} height={330} quality={100} alt="QR код билета" priority={true}/>
            </div>
            <div className={styles.buttons_container}>
                <Button type="submit" kind="submit" content="Скачать билет" onClick={() => handleDownloadTicket()}/>
                <Button type="submit" kind="secondary"
                        content={"Отправить на почту"}/>
            </div>
        </>
    )
}